﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SportManager
{
    public partial class AntropometricDlg : Form
    {
        private Guid? DataGuid = null;
        private AntropometricalData m_result;
        public AntropometricalData Result 
        {
            get
            { 
                return m_result;
            }
        }

        public AntropometricDlg()
        {
            InitializeComponent();
        }

        double GetDoubleValue(AntropometricalData data, string propertyName)
        { 
            double result = 0;
            double? value = data.Items[propertyName].Value;
            if (value.HasValue == true)
            {
                result = value.Value;
            }

            return result;
        }

        private double? GetNullOrDoubleValue(string value)
        {
            double? result = null;
            if (value.Length != 0)
            {
                result = double.Parse(value);
            }

            return result;
        }

        string GetStringValue(AntropometricalData data, string propertyName)
        {
            double value = GetDoubleValue(data, propertyName);
            return (value == 0 ? "" : value.ToString());
        }

        public AntropometricDlg(AntropometricalData data)
        {
            InitializeComponent();

            DataGuid = data.Id;
            dateTimePicker1.Value = data.Date;
            numericUpDownHeight.Value = (decimal)GetDoubleValue(data, "Height");
            numericUpDownMuscle.Value = (decimal)GetDoubleValue(data, "PercentOfMuscules");
            numericUpDownFat.Value    = (decimal)GetDoubleValue(data, "PercentOfFat");

            textBoxWeight.Text  = GetStringValue(data, "Weight");
            textBoxAnkle.Text   = GetStringValue(data, "AnkleSize");
            textBoxBiceps.Text  = GetStringValue(data, "BicepsSize");
            textBoxChest.Text   = GetStringValue(data, "ChestSize");
            textBoxForearm.Text = GetStringValue(data, "ForearmSize");
            textBoxNeck.Text    = GetStringValue(data, "NeckSize");
            textBoxPelvis.Text  = GetStringValue(data, "PelvisSize");
            textBoxShin.Text    = GetStringValue(data, "ShinSize");
            textBoxThight.Text  = GetStringValue(data, "ThightSize");
            textBoxWaist.Text   = GetStringValue(data, "WaistSize");
            textBoxWrist.Text   = GetStringValue(data, "WristSize");
        }

        private void AntropometricDlg_Load(object sender, EventArgs e)
        {
            dialogHeader1.SetHeaderText("Добавление антропометрических параметров");
            dialogHeader1.SetDescriptionText("Задайте необходимые параметры и нажмите ОК");
            dialogHeader1.SetIcon(Resource.ruler_icon32);
        }        

        private void button1_Click(object sender, EventArgs e)
        {
            m_result = new AntropometricalData();
            m_result.Items["AnkleSize"].Value   = GetNullOrDoubleValue(textBoxAnkle.Text);
            m_result.Items["BicepsSize"].Value  = GetNullOrDoubleValue(textBoxBiceps.Text);
            m_result.Items["ChestSize"].Value   = GetNullOrDoubleValue(textBoxChest.Text);
            m_result.Items["ForearmSize"].Value = GetNullOrDoubleValue(textBoxForearm.Text);
            m_result.Items["NeckSize"].Value    = GetNullOrDoubleValue(textBoxNeck.Text);
            m_result.Items["PelvisSize"].Value  = GetNullOrDoubleValue(textBoxPelvis.Text);
            m_result.Items["ShinSize"].Value    = GetNullOrDoubleValue(textBoxShin.Text);
            m_result.Items["ThightSize"].Value  = GetNullOrDoubleValue(textBoxThight.Text);
            m_result.Items["WaistSize"].Value   = GetNullOrDoubleValue(textBoxWaist.Text);
            m_result.Items["WristSize"].Value   = GetNullOrDoubleValue(textBoxWrist.Text);

            m_result.Date = dateTimePicker1.Value;

            m_result.Items["Height"].Value = (double)numericUpDownHeight.Value;
            m_result.Items["Weight"].Value = GetNullOrDoubleValue(textBoxWeight.Text);

            m_result.Items["PercentOfFat"].Value      = (double)numericUpDownFat.Value;
            m_result.Items["PercentOfMuscules"].Value = (double)numericUpDownMuscle.Value;

            if (DataGuid.HasValue == true)
            {
                m_result.Id = DataGuid.Value;
            }
            else
            {
                m_result.Id = Guid.NewGuid();
            }
        }

        private bool IsValueDoubleOk(string text, double min, double max)
        {
            if (text.Length != 0)
            { 
                double value;
                try
                {
                    value = double.Parse(text/*, out value*/);
                }
                catch
                {
                    return false;
                }

                return (value > min && value < max);
            }

            return true;
        }

        private void ValidateDoubleData(object obj, double min, double max)
        {
            if (IsValueDoubleOk((obj as TextBox).Text, min, max) == false)
            {
                errorProvider1.SetError((System.Windows.Forms.Control)obj, "Неверное значение");
                button1.Enabled = false;
            }
            else
            {
                errorProvider1.Clear();
                button1.Enabled = true;
            }
        }

        private void textBoxChest_Validated(object sender, EventArgs e)
        {
            ValidateDoubleData(sender, 50, 200);
        }

        private void textBoxBiceps_Validated(object sender, EventArgs e)
        {
            ValidateDoubleData(sender, 15, 80);
        }

        private void textBoxForearm_Validated(object sender, EventArgs e)
        {
            ValidateDoubleData(sender, 10, 50);
        }

        private void textBoxWrist_Validated(object sender, EventArgs e)
        {
            ValidateDoubleData(sender, 7, 30);
        }

        private void textBoxNeck_Validated(object sender, EventArgs e)
        {
            ValidateDoubleData(sender, 20, 100);
        }

        private void textBoxWaist_Validated(object sender, EventArgs e)
        {
            ValidateDoubleData(sender, 40, 300);
        }

        private void textBoxPelvis_Validated(object sender, EventArgs e)
        {
            ValidateDoubleData(sender, 40, 300);
        }

        private void textBoxThight_Validated(object sender, EventArgs e)
        {
            ValidateDoubleData(sender, 20, 100);
        }

        private void textBoxShin_Validated(object sender, EventArgs e)
        {
            ValidateDoubleData(sender, 15, 75);
        }

        private void textBoxAnkle_Validated(object sender, EventArgs e)
        {
            ValidateDoubleData(sender, 10, 40);
        }

        private void textBoxWeight_TextChanged(object sender, EventArgs e)
        {
            ValidateDoubleData(sender, 10, 300);
        }

        private void ShowStatisticDialog(string property)
        {
            StatisticDlg dlg = new StatisticDlg(property);
            dlg.ShowDialog();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowStatisticDialog("Weight");
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowStatisticDialog("Height");
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowStatisticDialog("PercentOfFat");
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowStatisticDialog("PercentOfMuscules");
        }

        private void linkLabel5_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowStatisticDialog("ChestSize");
        }

        private void linkLabel6_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowStatisticDialog("BicepsSize");
        }

        private void linkLabel7_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowStatisticDialog("ForearmSize");
        }

        private void linkLabel8_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowStatisticDialog("WristSize");
        }

        private void linkLabel9_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowStatisticDialog("NeckSize");
        }

        private void linkLabel10_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowStatisticDialog("WaistSize");
        }

        private void linkLabel11_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowStatisticDialog("PelvisSize");
        }

        private void linkLabel12_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowStatisticDialog("ThightSize");
        }

        private void linkLabel13_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowStatisticDialog("ShinSize");
        }

        private void linkLabel14_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowStatisticDialog("AnkleSize");
        }
    }
}
